<template>
  <view v-if="!isFirstload" class="container" :style="appThemeStyle">
    <!-- 页面头部 -->
    <view class="main-header" :style="{ height: platform == 'H5' ? '260rpx' : '320rpx', paddingTop: platform == 'H5' ? '0' : '80rpx' }">
      <image class="bg-image" src="/static/background/user-header2.png" mode="scaleToFill"></image>
      <!-- 用户信息 -->
      <view v-if="isLogin" class="user-info">
        <view class="user-avatar" @click="handlePersonal()">
          <avatar-image :url="userInfo.avatar_url" :width="100" />
        </view>
        <view class="user-content">
          <!-- 会员昵称 -->
          <!-- <view class="nick-name oneline-hide" @click="handlePersonal()">{{ userInfo.nick_name }}</view> -->
          <view class="nick-name oneline-hide" @click="handlePersonal()">{{ userInfo.mobile }}</view>
          <!-- 会员等级 -->
          <view v-if="$checkModule('user-grade') && userInfo.identity > 0 " class="user-grade">
            <view class="user-grade_icon">
              <image class="image"
                src="">
              </image>
            </view>
            <view class="user-grade_name">
              <text>{{ userInfo.identity_title }}</text>
            </view>
          </view>
		  <!-- 会员无等级时显示手机号 -->
		  <view v-else class="mobile">{{ userInfo.mobile }}</view>
		  
			<view v-if="userInfo.region " class="user-grade">
				<view class="user-grade_icon">
				  <image class="image"
					src="">
				  </image>
				</view>
				<view class="user-grade_name">
				  <text>{{ userInfo.region.name }}代理</text>
				</view>
			</view>
			<view v-if="userInfo.store_user_id>0" class="user-grade" style="background-color: #711b4b;">
				<view class="user-grade_icon">
				  <image class="image"
					src="">
				  </image>
				</view>
				<view class="user-grade_name">
				  <text>供货商</text>
				</view>
			</view>
		</view>
		<view  v-if="userInfo.identity!=1" class="user-content-reg"> 
		  <!-- <view class="login-tips-reg"  @click="handleReg">推荐码</view> -->
		  <view class="login-tips-reg"  @click="$navTo('pages/dealer/poster')">推荐码</view>
		</view>
      </view>
      <!-- 未登录 -->
      <view v-else class="user-info">
        <view class="user-avatar"  @click="handleLogin">
          <avatar-image :width="100" />
        </view>
        <view class="user-content">
          <view class="nick-name"  @click="handleLogin">未登录</view>
		  <view class="login-tips"  @click="handleLogin">点击登录账号</view>
        </view>
     
      </view>
    </view>

    <!-- 绑定手机号 -->
    <view v-if="isLogin && !userInfo.mobile && setting[SettingKeyEnum.REGISTER.value].isManualBind" class="my-mobile"
      @click="handleBindMobile()">
      <view class="info">点击绑定手机号，确保账户安全</view>
      <view class="btn-bind">去绑定</view>
    </view>

    <!-- 我的钱包 -->
    <view class="my-asset">
      <view class="asset-left flex-box dis-flex flex-x-around">
        <view class="asset-left-item" style="max-width: 200rpx;" @click="onTargetWallet('shopping')">
          <view class="item-value dis-flex flex-x-center">
            <text class="oneline-hide">{{ isLogin ? userInfo.shopping_coin : '--' }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>购物币充值</text>
          </view>
        </view>
        <view class="asset-left-item" @click="onTargetWallet('dec')">
          <view class="item-value dis-flex flex-x-center">
            <text class="oneline-hide">{{ isLogin ? userInfo.dec_coin : '--' }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
			<text>购物券充值</text>
          </view>
        </view>
        <view class="asset-left-item" @click="handleService({url:'pages/rec-list/index'})">
          <view class="item-value dis-flex flex-x-center">
            <text class="oneline-hide">{{ isLogin ? userInfo.rec_nums : '--' }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>直推人数</text>
          </view>
        </view>
      </view>
      <view class="asset-right">
        <!-- <view class="asset-left-item" @click="handleService({url:'pages/team-tree/index'})"> -->
        <view class="asset-left-item"  @click="openTree()">
          <view class="item-value dis-flex flex-x-center">
            <text class="oneline-hide">{{ isLogin ? userInfo.team_num : '--' }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>团队人数</text>
          </view>
        </view>
      </view>
    </view>
    <view class="my-asset">
      <view class="asset-left flex-box dis-flex flex-x-around">
        <view class="asset-left-item" style="max-width: 200rpx;" @click="handleService({url:'pages/finance/flow-dec-coin',query:{type:'all'}})">
          <view class="item-value dis-flex flex-x-center">
            <text class="oneline-hide">{{ isLogin ? userInfo.dec_coin_sum : '--' }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>累计购物券</text>
          </view>
        </view>
        <view class="asset-left-item" @click="handleService({url:'pages/finance/flow-dec-coin',query:{type:'freeze'}})">
          <view class="item-value dis-flex flex-x-center">
            <text class="oneline-hide">{{ isLogin ? userInfo.dec_coin_freeze : '--' }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
			<text>预估购物券</text>
          </view>
        </view>
        <view class="asset-left-item" @click="handleService({url:'pages/finance/flow-dec-coin',query:{type:'active'}})">
          <view class="item-value dis-flex flex-x-center">
            <text class="oneline-hide">{{ isLogin ? userInfo.dec_coin : '--' }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>可用购物劵</text>
          </view>
        </view>
      </view>
	  <view class="asset-right">
	    <!-- <view class="asset-left-item" @click="onTargetMyCoupon"> -->
	    <view class="asset-left-item">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? '0.00' : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>敬请期待!</text>
	      </view>
	    </view>
	  </view>
<!--      <view class="asset-right">
        <view class="asset-left-item" @click="handleService({url:'pages/finance/flow-dec-coin',query:{type:'freeze'}})">
          <view class="item-value dis-flex flex-x-center">
            <text class="oneline-hide">{{ isLogin ? userInfo.dec_coin_freeze : '--' }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>不可用购物劵</text>
          </view>
        </view>
      </view> -->
    </view>
	<view class="my-asset">
	  <view class="asset-left flex-box dis-flex flex-x-around">
	    <view class="asset-left-item" style="max-width: 200rpx;" @click="onTargetPoints('all')">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? userInfo.bonus_sum : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>累计积分</text>
	      </view>
	    </view>
	    <view class="asset-left-item" @click="onTargetPoints('freeze')">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? userInfo.bonus_freeze : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
			<text>预估积分</text>
	      </view>
	    </view>
	    <view class="asset-left-item" @click="onTargetPoints('active')">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? userInfo.bonus : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>可用积分</text>
	      </view>
	    </view>
	  </view>
	  <view  class="asset-right">
	    <!-- <view class="asset-left-item" @click="handleService({url:'pages/finance/flow-dec-coin',query:{type:'freeze'}})"> -->
	    <view class="asset-left-item">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? '0.00' : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>敬请期待!</text>
	      </view>
	    </view>
	  </view>
	</view>
	<view class="my-asset">
	  <view class="asset-left flex-box dis-flex flex-x-around">
	    <view class="asset-left-item" style="max-width: 200rpx;" @click="handleService({url:'pages/finance/flow-dec-coin',query:{type:'all','remark':'agent'}})">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? userInfo.agent_dec_coin_sum : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>区域代理累计收入</text>
	      </view>
	    </view>
	    <view class="asset-left-item" @click="handleService({url:'pages/finance/flow-dec-coin',query:{type:'freeze','remark':'agent'}})">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ?userInfo.agent_dec_coin_freeze: '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
			<text>区域代理预估收入</text>
	      </view>
	    </view>
	    <view class="asset-left-item" @click="handleService({url:'pages/finance/flow-dec-coin',query:{type:'active','remark':'agent'}})">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? userInfo.agent_dec_coin : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>区域代理可用收入</text>
	      </view>
	    </view>
	  </view>
	  <view class="asset-right">
	    <!-- <view class="asset-left-item" @click="onTargetMyCoupon"> -->
	    <view class="asset-left-item">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? '0.00' : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>敬请期待!</text>
	      </view>
	    </view>
	  </view>
	</view>
	<view class="my-asset">
	  <view class="asset-left flex-box dis-flex flex-x-around">
	    <view class="asset-left-item" style="max-width: 200rpx;" @click="handleService({url:'pages/finance/flow-money',query:{type:'all','model':'goods'}})">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? userInfo.goods_money_sum : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>货款累计收入</text>
	      </view>
	    </view>
	    <view class="asset-left-item"  @click="handleService({url:'pages/finance/flow-money',query:{type:'freeze','model':'goods'}})">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? userInfo.goods_money_freeze : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
			<text>货款预估收入</text>
	      </view>
	    </view>
	    <view class="asset-left-item" @click="handleService({url:'pages/finance/flow-money',query:{type:'active','model':'goods'}})">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? userInfo.goods_money  : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>货款可用收入</text>
	      </view>
	    </view>
	  </view>
	  <view class="asset-right">
	    <view class="asset-left-item">
	      <view class="item-value dis-flex flex-x-center">
	        <text class="oneline-hide">{{ isLogin ? '0.00' : '--' }}</text>
	      </view>
	      <view class="item-name dis-flex flex-x-center">
	        <text>敬请期待!</text>
	      </view>
	    </view>
	  </view>
	</view>
    <!-- 绑定手机号 (第2种样式) -->
    <!-- <view class="my-mobile2" @click="handleBindMobile()">
      <view class="info">点击绑定手机号，确保账户安全</view>
      <view class="btn-bind">去绑定</view>
    </view> -->

    <!-- 订单操作 -->
    <view class="order-navbar">
      <view class="order-navbar-item" v-for="(item, index) in orderNavbar" :key="index" @click="onTargetOrder(item)">
        <view class="item-icon">
          <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
        </view>
        <view class="item-name">{{ item.name }}</view>
        <view class="item-badge" v-if="item.count && item.count > 0">
          <text v-if="item.count <= 99" class="text">{{ item.count }}</text>
          <text v-else class="text">99+</text>
        </view>
      </view>
    </view>

    <!-- 我的服务 -->
    <view class="my-service">
      <view class="service-title">我的服务</view>
      <view class="service-content clearfix">
        <block v-for="(item, index) in service" :key="index">
          <view v-if="item.type == 'link' && item.enabled" class="service-item" @click="handleService(item)">
            <view class="item-icon">
              <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
            </view>
            <view class="item-name">{{ item.name }}</view>
            <view class="item-badge" v-if="item.count && item.count > 0">
              <text v-if="item.count <= 99" class="text">{{ item.count }}</text>
              <text v-else class="text">99+</text>
            </view>
          </view>
          <!-- 在线客服 -->
          <view v-if="item.type == 'contact' && item.enabled" class="service-item">
            <customer-btn>
              <view class="item-icon">
                <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
              </view>
              <view class="item-name">{{ item.name }}</view>
            </customer-btn>
          </view>
        </block>
      </view>
    </view>

<!-- 会员管理 -->
    <view class="my-service">
      <view class="service-title">会员管理</view>
      <view class="service-content clearfix">
        <block v-for="(item, index) in MemberManagement" :key="index">
          <view v-if="item.type == 'link' && item.enabled" class="service-item" @click="handleService(item)">
            <view class="item-icon">
              <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
            </view>
            <view class="item-name">{{ item.name }}</view>
            <view class="item-badge" v-if="item.count && item.count > 0">
              <text v-if="item.count <= 99" class="text">{{ item.count }}</text>
              <text v-else class="text">99+</text>
            </view>
          </view>
		  <view v-else="item.type == 'click' && item.enabled" class="service-item" @click="openTree()">
		    <view class="item-icon">
		      <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
		    </view>
		    <view class="item-name">{{ item.name }}</view>
		    <view class="item-badge" v-if="item.count && item.count > 0">
		      <text v-if="item.count <= 99" class="text">{{ item.count }}</text>
		      <text v-else class="text">99+</text>
		    </view>
		  </view>
        </block>
      </view>
    </view>
<!-- 财务管理 -->
    <view class="my-service">
      <view class="service-title">财务管理</view>
      <view class="service-content clearfix">
        <block v-for="(item, index) in financial" :key="index">
          <view v-if="item.type == 'link' && item.enabled" class="service-item" @click="handleService(item)">
            <view class="item-icon">
              <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
            </view>
            <view class="item-name">{{ item.name }}</view>
            <view class="item-badge" v-if="item.count && item.count > 0">
              <text v-if="item.count <= 99" class="text">{{ item.count }}</text>
              <text v-else class="text">99+</text>
            </view>
          </view>
        </block>
      </view>
    </view>
    <!-- 退出登录 -->
    <view v-if="isLogin" class="my-logout">
      <view class="logout-btn" @click="handleLogout()">
        <text>退出登录</text>
      </view>
    </view>
	<!-- 异步加载demo -->
	<next-tree :selectParent="false" :checkStrictly="true" :multiple="false" ref="nextTreeAsyncRef" :treeData="asyncTreeData" :loadData="loadData"/>

    <!-- 商品推荐 -->
    <recommended />

  </view>
</template>

<script>
  import store from '@/store'
  import { inArray } from '@/utils/util'
  import AvatarImage from '@/components/avatar-image'
  import Recommended from '@/components/recommended'
  import CustomerBtn from '@/components/customer-btn'
  import { setCartTabBadge } from '@/core/app'
  import SettingKeyEnum from '@/common/enum/setting/Key'
  import StoreModel from '@/common/model/Store'
  import SettingModel from '@/common/model/Setting'
  import * as UserApi from '@/api/user'
  import * as OrderApi from '@/api/order'

  import { checkLogin, filterModule } from '@/core/app'
  // 订单操作
  const orderNavbar = [
    { id: 'all', name: '全部订单', icon: 'qpdingdan' },
    { id: 'payment', name: '待支付', icon: 'daifukuan', count: 0 },
    { id: 'delivery', name: '待发货', icon: 'daifahuo', count: 0 },
    { id: 'received', name: '待收货', icon: 'daishouhuo', count: 0 },
  ]

  /**
   * 我的服务
   * id: 标识; name: 标题名称; icon: 图标; type 类型(link和button); url: 跳转的链接
   */
  const service = [
    { id: 'address', name: '收货地址', icon: 'shouhuodizhi', type: 'link', url: 'pages/address/index' },
    // { id: 'coupon', name: '领券中心', icon: 'lingquan', type: 'link', url: 'pages/coupon/index', moduleKey: 'market-coupon' },
    // { id: 'myCoupon', name: '优惠券', icon: 'youhuiquan', type: 'link', url: 'pages/my-coupon/index', moduleKey: 'market-coupon' },
    // { id: 'refund', name: '退换/售后', icon: 'shouhou', type: 'link', url: 'pages/refund/index', count: 0 },
    { id: 'contact', name: '在线客服', icon: 'kefu', type: 'contact' },
    { id: 'points', name: '我的积分', icon: 'jifen', type: 'link', url: 'pages/finance/flow-bonus', moduleKey: 'market-coupon',query:{type:'all'} },
    { id: 'dealer', name: '分销中心', icon: 'fenxiao', type: 'link', url: 'pages/dealer/index', moduleKey: 'apps-dealer'  },
    // { id: 'groupon', name: '我的拼团', icon: 'pintuan', type: 'link', url: 'pages/groupon/index?tab=1', moduleKey: 'apps-groupon' },
    // { id: 'bargain', name: '我的砍价', icon: 'kanjia', type: 'link', url: 'pages/bargain/index?tab=1', moduleKey: 'apps-bargain' },
    // { id: 'orderCenter', name: '订单中心', icon: 'order-c', type: 'link', url: 'pages/order/center' },
    { id: 'help', name: '我的帮助', icon: 'bangzhu', type: 'link', url: 'pages/help/index', moduleKey: 'content-help' },
  ]
  /**
   * 会员管理
   * id: 标识; name: 标题名称; icon: 图标; type 类型(link和button); url: 跳转的链接
   */
  const MemberManagement = [
    { id: 'team-tree', name: '接点列表', icon: 'shouhuodizhi', type: 'click', url: 'pages/team-tree/index', moduleKey: 'team-tree' },
    { id: 'rec-list', name: '推荐人列表', icon: 'lingquan', type: 'link', url: 'pages/rec-list/index', moduleKey: 'rec-list' },
  ]
  /**
   * 财务管理
   * id: 标识; name: 标题名称; icon: 图标; type 类型(link和button); url: 跳转的链接
   */
  const financial = [
    { id: 'bonus-to-shopping-coin', name: '积分转购物币', icon: 'shouhuodizhi', type: 'link', url: 'pages/finance/bonus-to-shopping-coin', moduleKey: 'bonus-to-shopping-coin' },
    { id: 'shopping', name: '购物币转账', icon: 'lingquan', type: 'link', url: 'pages/finance/shopping', moduleKey: 'shopping' },
    { id: 'shopping-coin', name: '购物币流水', icon: 'youhuiquan', type: 'link', url: 'pages/finance/shopping-coin', moduleKey: 'shopping-coin',query:{type:'all'} },
    { id: 'flow-dec-coin', name: '购物券流水', icon: 'shouhou', type: 'link', url: 'pages/finance/flow-dec-coin', moduleKey: 'flow-dec-coin' ,query:{type:'all'}},
    { id: 'withdraw-apply', name: '积分提现申请', icon: 'kefu', type: 'link', url: 'pages/finance/withdraw-apply', moduleKey: 'withdraw-apply' },
    { id: 'withdraw-log', name: '积分提现明细', icon: 'jifen', type: 'link', url: 'pages/finance/withdraw-log', moduleKey: 'withdraw-log' },
	{ id: 'money-to-shopping-coin', name: '货款转购物币', icon: 'shouhuodizhi', type: 'link', url: 'pages/finance/money-to-shopping-coin', moduleKey: 'money-to-shopping-coin' ,query:{model:"goods"}},
	{ id: 'money-withdraw-apply', name: '货款提现申请', icon: 'kefu', type: 'link', url: 'pages/finance/money-withdraw-apply', moduleKey: 'money-withdraw-apply' ,query:{model:"goods"}},
	{ id: 'money-withdraw-log', name: '货款提现明细', icon: 'jifen', type: 'link', url: 'pages/finance/money-withdraw-log', moduleKey: 'money-withdraw-log' ,query:{model:"goods"}},
	{ id: 'flow-money', name: '货款流水', icon: 'youhuiquan', type: 'link', url: 'pages/finance/flow-money', moduleKey: 'flow-money',query:{type:'all',model:"goods"} },
	{ id: 'coin-to-shopping-coin', name: '券转购物币', icon: 'shouhuodizhi', type: 'link', url: 'pages/finance/coin-to-shopping-coin', moduleKey: 'coin-to-shopping-coin' },
  ]
  export default {
    components: {
      AvatarImage,
      Recommended,
      CustomerBtn
    },
    data() {
      return {
        inArray,
        // 枚举类
        SettingKeyEnum,
        // 正在加载
        isLoading: true,
        // 首次加载
        isFirstload: true,
        // 是否已登录
        isLogin: false,
        // 系统设置
        setting: {},
        // 当前用户信息
        userInfo: {},
        // 账户资产
        assets: { balance: '--', points: '--', coupon: '--' },
        // 我的服务
        service,
		// 会员管理
		MemberManagement,
		// 财务管理
		financial,
        // 订单操作
        orderNavbar,
        // 当前用户待处理的订单数量
        todoCounts: { payment: 0, deliver: 0, received: 0 },
		//树形菜单
		multiple: false,
		selectParent: false,
		checkStrictly: false,
		localData:{},
		asyncTreeData: [],
      }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onLoad(options) {
		
	},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow(options) {
      this.onRefreshPage()
    },
	created() {
        self = this
    },
    methods: {

      // 刷新页面
      onRefreshPage() {
        // 更新购物车角标
        setCartTabBadge()
        // 判断是否已登录
        this.isLogin = checkLogin()
		if(!this.isLogin){
			this.handleLogin()
		}
        // 获取页面数据
        this.getPageData()
      },

      // 获取页面数据
      getPageData(callback) {
        const app = this
        app.isLoading = true
        Promise.all([app.getSetting(), app.getUserInfo(), app.getUserAssets(), app.getTodoCounts()])
          .then(result => {
            app.isFirstload = false
            // 初始化我的服务数据
            app.initService()
			// 初始化会员管理
            app.initMemberManagement()
			// 初始化财务管理
			app.initFinancial()
            // 初始化订单操作数据
            app.initOrderTabbar()
			//初始化团队数据
			app.initTeamTreeData()
            // 执行回调函数
            callback && callback()
          })
          .catch(err => console.log('catch', err))
          .finally(() => app.isLoading = false)
      },
	  
	  async initTeamTreeData(){
		const app = this
		if(!app.isLogin){
			return
		}
		UserApi.team({userId:app.userInfo.user_id,deep:1}, { load: app.isFirstload })
			.then(result => {
				app.asyncTreeData = result.data.data
			})
			.catch(err => {
				if (err.result && err.result.status == 401) {
					console.log(err.reuslt)
				} else {
				}
			})
	  },

      // 初始化我的服务数据
      async initService() {
        const app = this
        const isEnabledDealer = await StoreModel.isEnabledDealer()
        const isShowCustomerBtn = await SettingModel.isShowCustomerBtn()
        const newService = []
        service.forEach(item => {
          // 默认开启
          item.enabled = true
          // 我的积分
          if (item.id === 'points') {
            item.name = '我的' + app.setting[SettingKeyEnum.POINTS.value].points_name
          }
          // 是否显示分销中心
          if (item.id === 'dealer' && !isEnabledDealer) {
            item.enabled = false
          }
          // 企业微信客服
          if (item.id === 'contact' && !isShowCustomerBtn) {
            item.enabled = false
          }
          // 数据角标
          if (item.count != undefined) {
            item.count = app.todoCounts[item.id]
          }
          newService.push(item)
        })
        app.service = filterModule(newService)
      },
      // 初始化会员管理
      async initMemberManagement() {
        const app = this
        const isEnabledDealer = await StoreModel.isEnabledDealer()
        const isShowCustomerBtn = await SettingModel.isShowCustomerBtn()
        const newMemberManagement = []
        MemberManagement.forEach(item => {
          // 默认开启
          item.enabled = true
          // 数据角标
          if (item.count != undefined) {
            item.count = app.todoCounts[item.id]
          }
          newMemberManagement.push(item)
        })
        // app.MemberManagement = filterModule(newMemberManagement)
        app.MemberManagement = newMemberManagement
      },
      // 初始化财务管理
      async initFinancial() {
        const app = this
        const isEnabledDealer = await StoreModel.isEnabledDealer()
        const isShowCustomerBtn = await SettingModel.isShowCustomerBtn()
        const newFinancial = []
        financial.forEach(item => {
          // 默认开启
          item.enabled = true
          // 数据角标
          if (item.count != undefined) {
            item.count = app.todoCounts[item.id]
          }
          newFinancial.push(item)
        })
        // app.financial = filterModule(newFinancial)
        app.financial = newFinancial
      },

      // 初始化订单操作数据
      initOrderTabbar() {
        const app = this
        const newOrderNavbar = []
        orderNavbar.forEach(item => {
          if (item.count != undefined) {
            item.count = app.todoCounts[item.id]
          }
          newOrderNavbar.push(item)
        })
        app.orderNavbar = newOrderNavbar
      },

      // 获取商城设置
      getSetting() {
        const app = this
        return new Promise((resolve, reject) => {
          SettingModel.data()
            .then(setting => {
              app.setting = setting
              resolve(setting)
            })
            .catch(reject)
        })
      },

      // 获取当前用户信息
      getUserInfo() {
        const app = this
        return new Promise((resolve, reject) => {
          !app.isLogin ? resolve(null) : UserApi.info({}, { load: app.isFirstload })
            .then(result => {
              app.userInfo = result.data.userInfo
              resolve(app.userInfo)
            })
            .catch(err => {
              if (err.result && err.result.status == 401) {
                app.isLogin = false
                resolve(null)
              } else {
                reject(err)
              }
            })
        })
      },

      // 获取账户资产
      getUserAssets() {
        const app = this
        return new Promise((resolve, reject) => {
          !app.isLogin ? resolve(null) : UserApi.assets({}, { load: app.isFirstload })
            .then(result => {
              app.assets = result.data.assets
              resolve(app.assets)
            })
            .catch(err => {
              if (err.result && err.result.status == 401) {
                app.isLogin = false
                resolve(null)
              } else {
                reject(err)
              }
            })
        })
      },

      // 获取当前用户待处理的订单数量
      getTodoCounts() {
        const app = this
        return new Promise((resolve, reject) => {
          !app.isLogin ? resolve(null) : OrderApi.todoCounts({}, { load: app.isFirstload })
            .then(result => {
              app.todoCounts = result.data.counts
              resolve(app.todoCounts)
            })
            .catch(err => {
              if (err.result && err.result.status == 401) {
                app.isLogin = false
                resolve(null)
              } else {
                reject(err)
              }
        })
            })
      },

      // 跳转到登录页
      handleLogin() {
        !this.isLogin && this.$navTo('pages/login/index')
      },
	  
	  handleReg() {
	    this.isLogin && this.$navTo('pages/reg/index',{mobile:this.userInfo.mobile})
	  },

      // 跳转到绑定手机号页面
      handleBindMobile() {
        this.$navTo('pages/user/bind/index')
      },

      // 跳转到修改个人信息页
      handlePersonal() {
        this.$navTo('pages/user/personal/index')
      },


	// 跳转到首页
    handleHomeUrl() {
        this.$navTo('pages/index/index')
    },

      // 退出登录
      handleLogout() {
        const app = this
        uni.showModal({
          title: '友情提示',
          content: '您确定要退出登录吗?',
          success(res) {
            if (res.confirm) {
              store.dispatch('Logout', {})
                // .then(result => app.onRefreshPage())
                .then(result => app.handleHomeUrl())
            }
          }
        })
      },

      // 跳转到钱包页面
      onTargetWallet(type=null) {
        this.$navTo('pages/wallet/index',{type})
      },

      // 跳转到订单页
      onTargetOrder(item) {
        this.$navTo('pages/order/index', { dataType: item.id })
      },

      // 跳转到我的积分页面
      onTargetPoints(type) {
        // this.$navTo('pages/points/log')
        this.$navTo('pages/finance/flow-bonus',{type})
      },

      // 跳转到我的优惠券页
      onTargetMyCoupon() {
        this.$navTo('pages/my-coupon/index')
      },

		// 跳转到服务页面
		handleService({ url,query }) {
			if(query!==undefined){
				// if(query.model !==undefined && query.model=="goods" && this.userInfo.store_user_id<=0){
				// 	this.$toast('您还不是供货商');return false;
				// }
				
				if(query.remark !==undefined && query.remark=="agent" && !this.userInfo.region){
					this.$toast('您还不是区域代理');return false;
				}
			}
			
			this.$navTo(url,query)
		},

      // 在线客服
      handleContact() {
        // 商城客服设置
        const setting = this.setting[SettingKeyEnum.CUSTOMER.value]
        // 企业微信客服
        if (setting.provider == 'wxqykf') {
          if (!setting.config.wxqykf.url || !setting.config.wxqykf.corpId) {
            this.$toast('客服链接和企业ID不能为空')
            return
          }
          // #ifdef H5
          window.open(setting.config.wxqykf.url)
          // #endif
          // #ifdef MP-WEIXIN
          wx.openCustomerServiceChat({
            extInfo: { url: setting.config.wxqykf.url },
            corpId: setting.config.wxqykf.corpId,
            success(res) {}
          })
          // #endif
        }
      },
	  openTree: function() {
		  if(!this.isLogin){
			  return
		  }
	      this.$refs.nextTreeAsyncRef.showTree = true
	  },
	  loadData(nodeItem) {
	      // 同步实现的代码处理方式 可以返回单个子节点的集合也可以返回子孙节点的集合
	      // 如果期望子集节点中还存在孙子节点可以打开，请在初始化数据的时候，初始化个空数组的子节点配置值{[this.childrenKey]: []}
	  
	      // if(nodeItem && this.localData[nodeItem.id]) {
	      //  return this.localData[nodeItem.id]
	      // } else {
	      //  return []
	      // }
	      // 异步的代码实现方式 可以返回单个子节点的集合也可以返回子孙节点的集合
	      // 如果期望子集节点中还存在孙子节点可以打开，请在初始化数据的时候，初始化个空数组的子节点配置值{[this.childrenKey]: []}
	      // return new Promise((resolve, reject) => {
	      //     setTimeout(() => {
	      //         if(nodeItem && self.localData[nodeItem.id]) {
	      //             return resolve(self.localData[nodeItem.id])
	      //         } else {
	      //             return resolve([])
	      //         }
	      //     }, 1000)
	      // }) 
		  
		  const app = this
		  return new Promise((resolve, reject) => {
		    !app.isLogin ? resolve(null) : UserApi.team({userId:nodeItem.id,deep:2}, { load: app.isFirstload })
		      .then(result => {
				app.localData = Object.assign(app.localData,result.data.data)
				if(nodeItem && app.localData[nodeItem.id]) {
					return resolve(app.localData[nodeItem.id])
				} else {
					return resolve([])
				}
		      })
		      .catch(err => {
		        if (err.result && err.result.status == 401) {
		          resolve(null)
		        } else {
		          reject(err)
		        }
		      })
		  })
	  },
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh() {
      // 获取首页数据
      this.getPageData(() => {
        uni.stopPullDownRefresh()
      })
    },


  }
</script>

<style lang="scss" scoped>
  .container {
    padding-bottom: 60rpx;
  }

  // 页面头部
  .main-header {
    // background-color: #FBF7EF;
    position: relative;
    width: 100%;
    height: 280rpx;
    background-size: 100% 100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    padding-left: 30rpx;

    .bg-image {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 0;
    }

    .user-info {
      display: flex;
      height: 130rpx;
      z-index: 1;
	  align-items: center;

      .user-content-reg {
        display: flex;
        flex-direction: column;
        justify-content: right;
        margin-left: 30rpx;
        color: #c59a46;
		
		.login-tips-reg {
		  margin-top: 12rpx;
		  font-size: 30rpx;
		  text-align: right;
		  width: 320rpx;
		}
		
		}


      .user-content {
        display: flex;
        flex-direction: column;
        justify-content: center;
        margin-left: 30rpx;
        color: #c59a46;

        .nick-name {
          font-size: 34rpx;
          font-weight: bold;
          max-width: 300rpx;
        }

        .mobile {
          margin-top: 15rpx;
          font-size: 28rpx;
        }

        .user-grade {
          align-self: baseline;
          display: flex;
          align-items: center;
          background: #3c3c3c;
          margin-top: 12rpx;
          border-radius: 10rpx;
          padding: 4rpx 12rpx;

          .user-grade_icon .image {
            display: block;
            width: 32rpx;
            height: 32rpx;
          }

          .user-grade_name {
            margin-left: 5rpx;
            font-size: 26rpx;
            color: #EEE0C3;
          }

        }

        .login-tips {
          margin-top: 12rpx;
          font-size: 30rpx;
		  float: left;
		  max-width: 300rpx;
        }
		


      }
    }
  }

  // 角标组件
  .item-badge {
    position: absolute;
    top: 0;
    right: 55rpx;
    // background: $main-bg;
    background: #fa2209;
    color: #fff;
    border-radius: 100%;
    min-width: 38rpx;
    height: 38rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 1rpx;
    font-size: 24rpx;
  }

  // 我的钱包
  .my-asset {
    display: flex;
    background: #fff;
    padding: 40rpx 0;

    .asset-right {
      width: 200rpx;
      border-left: 1rpx solid #eee;
    }

    .asset-right-item {
      text-align: center;
      color: #545454;

      .item-icon {
        font-size: 44rpx;
      }

      .item-name {
        margin-top: 14rpx;
        font-size: 28rpx;
      }

    }

    .asset-left-item {
      max-width: 183rpx;
      text-align: center;
      color: #666;
      padding: 0 16rpx;

      .item-value {
        font-size: 34rpx;
        color: $main-bg;
      }

      .item-name {
        margin-top: 14rpx;
        font-size: 28rpx;
      }

    }

  }

  // 订单操作
  .order-navbar {
    display: flex;
    margin: 20rpx auto 20rpx auto;
    padding: 20rpx 0 26rpx 0;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    font-size: 30rpx;
    border-radius: 5rpx;
    background: #fff;

    &-item {
      position: relative;
      width: 25%;

      .item-icon {
        text-align: center;
        margin: 0 auto;
        padding: 10rpx 0;
        color: #545454;
        font-size: 44rpx;
      }

      .item-name {
        font-size: 28rpx;
        color: #545454;
        text-align: center;
        margin-right: 10rpx;
      }

    }
  }

  // 我的服务
  .my-service {
    margin: 22rpx auto 22rpx auto;
    padding: 22rpx 0;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    border-radius: 5rpx;
    background: #fff;

    .service-title {
      padding-left: 24rpx;
      margin-bottom: 20rpx;
      font-size: 30rpx;
    }

    .service-content {

      margin-bottom: -20rpx;

      .service-item {
        position: relative;
        width: 25%;
        float: left;
        margin-bottom: 30rpx;

        .item-icon {
          text-align: center;
          margin: 0 auto;
          padding: 14rpx 0;
          color: $main-bg;
          font-size: 44rpx;
        }

        .item-name {
          font-size: 28rpx;
          color: #545454;
          text-align: center;
        }

      }
    }
  }

  // 退出登录
  .my-logout {
    display: flex;
    justify-content: center;
    margin-top: 50rpx;

    .logout-btn {
      width: 60%;
      margin: 0 auto;
      font-size: 28rpx;
      color: #616161;
      border-radius: 20rpx;
      border: 1px solid #dcdcdc;
      padding: 16rpx 0;
      text-align: center;
    }
  }

  // 绑定手机号 样式1
  .my-mobile {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16rpx 40rpx;
    background: #FCEBD1;

    .info {
      color: #cd8c0c;
      font-size: 28rpx;
    }

    .btn-bind {
      padding: 8rpx 24rpx;
      background-color: #EAB766;
      color: #fff;
      border-radius: 30rpx;
      font-size: 26rpx;
      text-align: center;
    }
  }

  // 绑定手机号 样式2
  .my-mobile2 {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 20rpx auto 20rpx auto;
    padding: 12rpx 40rpx;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    font-size: 30rpx;
    border-radius: 5rpx;
    background: #fff;

    .info {
      // color: #cd8c0c;
      font-size: 26rpx;
    }

    .btn-bind {
      padding: 8rpx 24rpx;
      background-color: #EAB766;
      color: #fff;
      border-radius: 30rpx;
      font-size: 26rpx;
      text-align: center;
    }
  }
</style>